﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Activity
{
    public class EditActivityModel
    {
        public int ActivityId { get; set; }

        [DisplayName("Nombre de la actividad")]
        [Required(ErrorMessage = "El nombre de la actividad es requerida")]
        [StringLength(50, ErrorMessage = "El nombre debe ser máximo de 50 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El nombre debe ser solo letras o números")]
        public string Name { get; set; }

        [DisplayName("Jefatura/Gerencia")]
        [Required(ErrorMessage = "Una jefatura/gerencia es necesaria")]
        public int Management { get; set; }

        [DisplayName("Proceso")]
        [Required(ErrorMessage = "El proceso es necesario")]
        public int Process { get; set; }

        [DisplayName("Area")]
        [Required(ErrorMessage = "El area es necesario")]
        public int Area { get; set; }

        public IEnumerable<SelectListItem> Managements
        {
            get
            {
                return managementList;
            }
        }

        public IEnumerable<SelectListItem> Processes
        {
            get
            {
                return processList;
            }
        }

        public IEnumerable<SelectListItem> Areas
        {
            get
            {
                return areaList;
            }
        }

        private List<SelectListItem> managementList;
        private List<SelectListItem> processList;
        private List<SelectListItem> areaList;

        riskanalysisEntities entities = new riskanalysisEntities();

        public EditActivityModel()
        {
            FillList();
        }

        public EditActivityModel(int areaId)
            : this()
        {
            FillList();
            FillActivityById(areaId);
        }

        private void FillList()
        {
            managementList = new List<SelectListItem>();

            var queryManagements = from m in entities.managements
                                   where m.enabled
                                   select m;

            foreach (management item in queryManagements)
            {
                managementList.Add(
                    new SelectListItem
                    {
                        Text = item.name,
                        Value = item.managementId.ToString()
                    });
            }
            processList = RegisterActivityModel.FillProcessList(queryManagements.First().managementId);
            areaList = RegisterActivityModel.FillAreaList(Convert.ToInt32(processList.First().Value));
        }

        private void FillActivityById(int id)
        {
            activity theActivity = GetActivityById(id);

            ActivityId = theActivity.activityId;
            Name = theActivity.name;
            Management = theActivity.area.process.managementId;
            processList = RegisterActivityModel.FillProcessList(Management);
            Process = theActivity.area.processId;
            areaList = RegisterActivityModel.FillAreaList(Process);
            Area = theActivity.areaId;
        }

        private activity GetActivityById(int id)
        {
            var query = from a in entities.activities
                        where a.activityId == id && a.enabled
                        select a;

            activity theActivity = query.FirstOrDefault();

            if (theActivity == null)
                throw new EditActivityException("La actividad que usted busca no existe");
            return theActivity;
        }

        public void Save()
        {
            activity theActivity = GetActivityById(ActivityId);

            theActivity.name = Name;
            theActivity.areaId = Area;         
            entities.SaveChanges();
        }
    }
}